import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {AppConfig} from "../../../../app/appConfig";
import {FoodHomeService} from "../food-home/food-home.service";
import {CommonService} from "../../../../app/common.service";

/**
 * 订单备注页面
 */

@IonicPage()
@Component({
  selector: 'page-food-remark',
  templateUrl: 'food-remark.html',
})
export class FoodRemarkPage {
  private basket: any;
  private tableId;
  private remark = '';
  private oldFoodList;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private appConfig: AppConfig,
              private httpService: FoodHomeService,
              private cs: CommonService) {
    this.basket = this.navParams.get('basket');
    this.tableId = this.navParams.get('tableId');
    this.oldFoodList = this.navParams.get('oldFoodList');
    console.log("food-remark1:");
    console.log(this.oldFoodList);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FoodRemarkPage');
  }

  getOrder() {
    this.appConfig.presentLoading("正在生成订单");
    let dId = localStorage.getItem('dId');
    if (!dId) {//生成订单前再次判断到底有没有设备号
      dId = this.cs.getUUID();
      localStorage.setItem('dId', dId);
    }
    //这里生成订单
    let orderList = {
      orderno: '',
      trandate: new Date(),
      tableno: this.tableId,
      totalprice: this.basket.allPrice,
      printflag: '',
      printtime: '',
      payflag: '0',
      paytype: '',
      discounttype: '',
      discountrate: 0.00,
      realpay: 0.00,
      orderstatus: '9',
      remark: this.remark,
      ordertype: 1,
      deviceid: dId
    };
    let orderDetail = [];
    for (let food of this.basket.foodsList) {
      orderDetail.push({
        foodid: food.fId,
        num: food.fNum
      })
    }
    if (this.oldFoodList == null || this.oldFoodList == undefined) {
      this.httpService.getOrder([orderList, orderDetail, '0', this.basket.rId]).subscribe(data => {
        this.appConfig.loader.dismiss();
        let temp: any = data;
        if (temp.code == 200) {
          let orderData = {
            orderNo: temp.data.orderno,
            orderDate: temp.data.trandate,
          };
          this.navCtrl.setRoot('FoodOrderPage', {
            basket: this.basket,
            orderData: orderData,
            isAdd: false
          });
        }
        else
          this.appConfig.presentToast(temp.msg);
      }, e => {
        this.cs.httpError(e);
        this.appConfig.loader.dismiss();
      });
    }
    else { //处理加菜
      this.httpService.addFoods({
        orderList: orderList,
        orderNo: this.oldFoodList.orderNo,
        orderDetail: orderDetail
      }).subscribe(data => {
        this.appConfig.loader.dismiss();
        let temp: any = data;
        if (temp.code == 200) {
          this.navCtrl.setRoot('FoodOrderPage', {
            orderNo:this.oldFoodList.orderNo,
            basket: [],
            isAdd: true
          });
        }
        else {
          this.appConfig.presentToast(temp.msg);
        }
      }, e => {
        this.cs.httpError(e);
        this.appConfig.loader.dismiss();
      });
    }
  }
}
